package cn.jiedh.app.meiweidk.Utils;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.OptionsPickerView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import cn.jiedh.app.meiweidk.R;

/**
 * 创建人 PACY
 * 创建时间 18/11/19 15:44.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述
 */

public class CityUtils {
    //城市列表选择
    OptionsPickerView optionsPickerView;

    ArrayList<String> options1Items = new ArrayList<>();
    ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();


    private static class CityHelper {
        private static CityUtils INSTANCE = new CityUtils();
    }


    public static CityUtils getInstance() {
        return CityHelper.INSTANCE;
    }

    //获取全国所有城市列表
    public void getAllCity(Context context, final CityNameInterface cityNameInterface) {


        AllAreaBean allAreaBean = null;

        try {
            InputStream inputStream = context.getAssets().open("AllCity.txt");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            String city = new String(buffer);

            allAreaBean = JSON.parseObject(city, AllAreaBean.class);


            //获取所有的省份名字
            for (int i = 0; i < allAreaBean.getProvince().size(); i++) {
                ProvinceBean provinceBean = new ProvinceBean();
                provinceBean.setId(i);
                provinceBean.setName(allAreaBean.getProvince().get(i).getName());
                options1Items.add(allAreaBean.getProvince().get(i).getName());

                //存放地区
                ArrayList<ArrayList<String>> listArea = new ArrayList<>();
                //存放城市名字
                ArrayList<String> listCity = new ArrayList<>();
                for (int j = 0; j < allAreaBean.getProvince().get(i).getCity().size(); j++) {
                    listCity.add(allAreaBean.getProvince().get(i).getCity().get(j).getName());

                    listArea.add((ArrayList<String>) allAreaBean.getProvince().get(i).getCity().get(j).getArea());
                }
                options3Items.add(listArea);
                options2Items.add(listCity);
            }


            optionsPickerView = new OptionsPickerView.Builder(context, (options1, options2, options3, v) -> {
                //返回的分别是三个级别的选中位置  判断省 和 城市 是否重名
                String area = "";
                if (options1Items.get(options1).equals(options2Items.get(options1).get(options2))) {
                    area = options1Items.get(options1) + " "
                            + options3Items.get(options1).get(options2).get(options3) + "  ";
                } else {
                    area = options1Items.get(options1) + " "
                            + options2Items.get(options1).get(options2) + " "
                            + options3Items.get(options1).get(options2).get(options3) + "  ";
                }
                //获取所有的城市名称
                cityNameInterface.getCityName(area);
            }).setOutSideCancelable(false)
                    .setDividerColor(R.color.hint3_text_color)
                    .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                    .setContentTextSize(20)
                    .setTitleText("选择城市")
                    .setTitleColor(Color.BLACK)
//                    .setLabels("省", "市", "区")//设置选择的三级单位
                    .setCyclic(false, false, false)//循环与否
                    .setSelectOptions(0, 0, 0)  //设置默认选中项
                    .build();

            optionsPickerView.setPicker(options1Items, options2Items, options3Items);


            optionsPickerView.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
